package com.nanbei.eduUcenter.controller;


import com.alibaba.nacos.client.utils.JSONUtils;
import com.nanbei.commonUntil.JwtUtils;
import com.nanbei.commonUntil.R;
import com.nanbei.commonUntil.vo.orderVo.UcenterMemberOrder;
import com.nanbei.eduUcenter.entity.UcenterMember;
import com.nanbei.eduUcenter.entity.vo.LoginCenterMember;
import com.nanbei.eduUcenter.entity.vo.RegistCenterMember;
import com.nanbei.eduUcenter.service.UcenterMemberService;
import com.nanbei.servicebase.exceptionHandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-01-19
 */
@RestController
@RequestMapping("/eduUcenter/centerMember")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    /**
     * 登录接口
     * @param loginCenterMember
     * @return
     */
    @PostMapping("/loginCenter")
    public R loginCenter(@RequestBody LoginCenterMember loginCenterMember){
        String token = ucenterMemberService.loginCenter(loginCenterMember);
        return R.ok().data("token",token);
    }


    /**
     * 注册账号
     * @param registCenterMember
     * @return
     */
    @PostMapping("/registerUser")
    public R registerUser(@RequestBody RegistCenterMember registCenterMember){
        ucenterMemberService.registerUser(registCenterMember);
        return R.ok();
    }

    /**
     * 登录根据token获取用户信息
     * @param request
     * @return
     */
    @GetMapping("/userInfo")
    public R userInfo(HttpServletRequest request){
        //根据token获取用户ID
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //根据用户ID查询用户信息
        UcenterMember ucenterMember = ucenterMemberService.getById(memberId);
        return R.ok().data("ucenterMember",ucenterMember);
    }


    /**
     * 评论根据token获取用户信息
     * @param request
     * @return
     */
    @GetMapping("/userInfoVo")
    public UcenterMember userInfoVo(HttpServletRequest request){
        //根据token获取用户ID
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //根据用户ID查询用户信息
        if (!StringUtils.isEmpty(memberId)){
            new GuliException(20001,"请先登录");
        }
        UcenterMember ucenterMember = ucenterMemberService.getById(memberId);
        return ucenterMember;
    }

    /**
     * 订单根据用户ID获取用户信息
     * @param userId
     * @return
     */
    @GetMapping("/orderUserInfo/{userId}")
    public UcenterMemberOrder orderUserInfo(@PathVariable String userId){
        UcenterMember ucenterMember = ucenterMemberService.getById(userId);
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(ucenterMember,ucenterMemberOrder);
        return ucenterMemberOrder;
    }


    /**
     * 根据日期统计一天的注册人数
     * @param day
     * @return
     */
    @GetMapping("/countRegister/{day}")
    public Integer countRegister(@PathVariable String day){
        Integer count = ucenterMemberService.countRegister(day);
        return count;
    }


}

